package cn.lili.controller.promotion;

import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.PointsGoods;
import cn.lili.modules.promotion.entity.dos.PointsGoodsBanner;
import cn.lili.modules.promotion.entity.dto.search.PointsGoodsSearchParams;
import cn.lili.modules.promotion.entity.vos.PointsGoodsBannerVO;
import cn.lili.modules.promotion.entity.vos.PointsGoodsVO;
import cn.lili.modules.promotion.service.PointsGoodsBannerService;
import cn.lili.modules.promotion.service.PointsGoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 积分商城banner接口
 **/
@RestController
@Api(tags = "积分商城banner接口")
@RequestMapping("/promotion/points/banner")
@RequiredArgsConstructor
public class PointsGoodsBannerController {

    private final PointsGoodsService pointsGoodsService;

    private final PointsGoodsBannerService pointsBannerService;

    @PutMapping(consumes = "application/json", produces = "application/json")
    @ApiOperation(value = "新增或修改积分商城轮播图")
    public ResultMessage<Object> updatePointsBanner(@RequestBody PointsGoodsBannerVO pointsGoodsVo) {
        Objects.requireNonNull(UserContext.getCurrentUser());
        if(StringUtils.isNotEmpty(pointsGoodsVo.getId())){
            pointsBannerService.updateById(pointsGoodsVo);
        }else{
            pointsBannerService.save(pointsGoodsVo);
        }
        return ResultUtil.success();
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除积分商城轮播图")
    public ResultMessage<Object> delete(@PathVariable String id) {
        if (pointsBannerService.removeById(id)){
            return ResultUtil.success();
        }
        throw new ServiceException(ResultCode.POINT_BANNER_ERROR);
    }

    @GetMapping
    @ApiOperation(value = "获取积分商城轮播图")
    public ResultMessage<List<PointsGoodsBanner>> getPointsGoodsBannerList() {
        List<PointsGoodsBanner> pointsBannerList = pointsBannerService.list();
        return ResultUtil.data(pointsBannerList);
    }

}
